package classloader;

import java.lang.reflect.Method;
/*
不同命名空间中，委托的父加载器为同一个应用类加载器时，加载同一个类，只会加载一次，不会重新加载
 */
public class MyTest20 {
    public static void main(String[] args) throws Exception {
        MyTest16 loader1 = new MyTest16("load1");
        MyTest16 loader2 = new MyTest16("load2");
        Class<?> clazz1 = loader1.loadClass("classloader.Person");
        Class<?> clazz2 = loader2.loadClass("classloader.Person");

        //clazz1和clazz均由应用类加载器加载的，第二次不会重新加载，结果为true
        System.out.println(clazz1 == clazz2);

        Object object1 = clazz1.newInstance();
        Object object2 = clazz2.newInstance();

        Method method = clazz1.getMethod("setPerson", Object.class);
        method.invoke(object1, object2);
    }
}

class Person {

    private Person person;

    public void setPerson(Object object) {
        this.person = (Person) object;
    }
}
